/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyCondition;

import java.util.ArrayList;
import StringJoinerCutter.*;
/**
 *
 * @author Welcome
 */
public class AndOrHandler {
    //<editor-fold defaultstate="collapsed" desc="1/Các thuộc tính cơ bản">
    private String conditionString;
    private ArrayList<String> andConditions;
    private ArrayList<String> orConditions;
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="2/Cac phương thức khởi tạo">
    public AndOrHandler(String conditionString) {
        this.conditionString = conditionString;
        andConditions = new ArrayList<String>();
        orConditions= new ArrayList<String>();
    }
    
    public AndOrHandler() {
        this.conditionString="";
        andConditions = new ArrayList<String>();
        orConditions= new ArrayList<String>();
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="3/ Cac phương thức Getter/Setter">
    
    public void setConditionString(String conditionString) {
        this.conditionString = conditionString;
    }

    public String getConditionString() {
        return conditionString;
    }

    public ArrayList<String> getAndConditions() {
        return andConditions;
    }
    
    public ArrayList<String> getOrConditions() {
        return orConditions;
    }
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="4/ Các phương thức xử lý nghiệp vụ">
    public void splitConditions()
    {
        String tempString = "and "+conditionString;
        if(conditionString.equals(""))
            return;
        
        
        while(!tempString.equals(""))
        {
            
            String str1 = tempString.substring(0,3);
            tempString=tempString.substring(3).trim();
            int tempPos = findNextAndOr(tempString);
            String resultStr="";
            if(tempPos==-1)
                resultStr=tempString.substring(0); 
            else
                resultStr= tempString.substring(0,tempPos).trim();
            
            if(str1.equals("and"))
            {
                andConditions.add(resultStr);
            }
            else
            {
                orConditions.add(resultStr);
            }
            
            tempString = tempString.substring(resultStr.length()).trim();
            /*
            String[] strs=tempString.split(resultStr);
            if(strs.length>0)
                tempString = strs[1].trim();
            else
                tempString="";
             * 
             */
        }
    }
    
    public int findNextAndOr(String str)
    {
        int result=-1;
        int tempAnd = str.indexOf("and");
        int tempOr = str.indexOf("or");
        
        if(tempAnd==-1 && tempOr==-1)
        {
            return result;
        }
        else
        if(tempAnd>-1 && tempOr>-1)
        {
            return tempAnd>tempOr?tempOr:tempAnd;
        }
        else
        {
            return tempAnd>-1?tempAnd:tempOr;
        }
    }
    
    public void showData()
    {
        System.out.println("and condition");
        for(int i=0; i<andConditions.size();i++)
        {
            System.out.println(andConditions.get(i));
        }
        System.out.println("or condition");
        for(int i=0; i<orConditions.size();i++)
        {
            System.out.println(orConditions.get(i));
        }
    }
    //</editor-fold>
    
    
}

